/*
 * @Author: chen17
 * @Date: 2022-03-30 11:45:06
 * @LastEditTime: 2023-12-17 20:17:18
 * @Description: 用户登录信息
 */

import router from '@/router'
import { login, getUserInfo } from '@/api/login'
import { getToken, setToken, removeToken } from '@/utils/token-manage'

const state = {
    token: getToken(),
    userInfo: sessionStorage.getItem('userInfo') ? JSON.parse(sessionStorage.getItem('userInfo')) : {}
}

const mutations = {
    SET_TOKEN: (state, data) => {
        if (data.token && data.userMark) {
            sessionStorage.setItem('userMark', data.userMark)
            state.token = data.token
            setToken(data.token)
        } else {
            removeToken()
            state.token = ''
            sessionStorage.removeItem('userMark')
        }
    },
    SET_USERINFO: (state, userinfo) => {
        if (JSON.stringify(userinfo) !== '{}') {
            state.userInfo = userinfo
            sessionStorage.setItem('userInfo', JSON.stringify(userinfo))
        } else {
            state.userInfo = userinfo
            sessionStorage.removeItem('userInfo')
        }
    }
}

const actions = {
    // 登录
    login({ commit, dispatch, rootGetters }, userInfo) {
        return new Promise(async (resolve, reject) => {
            login(userInfo)
                .then(async res => {
                    const { userId, access_token, token_type } = res.data
                    const accessToken = token_type + ' ' + access_token
                    // 设置登录状态
                    commit('SET_TOKEN', {
                        userMark: userId,
                        token: accessToken
                    })
                    // 初始化用户信息
                    dispatch('initUserInfo')
                    // 初次登录 - 初始化路由
                    if (rootGetters.authRoutes && rootGetters.authRoutes.length <= 0) await dispatch('permission/initAuthRoutes', {}, { root: true })
                    resolve(true)
                })
                .catch(error => {
                    reject(error)
                })
        })
    },
    // 退出
    logout({ commit, dispatch }) {
        return new Promise((resolve, reject) => {
            // 设置登录状态
            commit('SET_TOKEN', {
                userMark: '',
                token: ''
            })
            // 设置用户信息
            commit('SET_USERINFO', {})
            // 重置tabs
            dispatch('tabs/resetTabRoute', [], { root: true })
            // 重置权限的路由是否初始化
            dispatch('permission/resetAuthRoutes', [], { root: true })
            // 登录页面
            router.push('/login')
            // 刷新页面
            // window.location.reload()
            resolve()
        })
    },
    // 401 || 权限
    resetToken({ commit, dispatch }, needRedirct) {
        return new Promise(resolve => {
            // 清除token
            commit('SET_TOKEN', {
                userMark: '',
                token: ''
            })
            // 清空用户信息
            commit('SET_USERINFO', {})
            // 重置tabs
            dispatch('tabs/resetTabRoute', [], { root: true })
            // 重置权限的路由是否初始化
            dispatch('permission/resetAuthRoutes', [], { root: true })
            // 登录页面
            router.push('/login')
            // window.location.reload()
            resolve()
        })
    },
    // 初始化用户信息
    initUserInfo({ commit, dispatch }) {
        getUserInfo()
            .then(res => {
                // 设置用户信息
                commit('SET_USERINFO', res.data)
            })
            .catch(error => {})
    }
}

export default {
    namespaced: true,
    state,
    mutations,
    actions
}
